package com.yanqu.road.server.cmd.activity.dunhuangshiku;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroup;
import com.yanqu.road.logic.bussiness.player.UserPatronsBussiness;
import com.yanqu.road.logic.helper.UnionActivityGroupHelper;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.dunhuangshiku.DunHuangShiKuProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.manger.activity.dunhuangshiku.DunHuangShiKuMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;
import io.netty.channel.ChannelHandlerContext;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description
 * @Author cwq
 * @Data 2021/11/27 15:42
 */
@Cmd(code = GameProtocol.S_DUN_HUANG_SHI_KU_GET_ACTIVE_USER_INFO_FROM_CROSS, desc = "敦煌石窟 向区服获取活跃玩家数据 跨服返回区服 协议")
public class DunHuangShikuGetActiveUserInfoFromCrossCmd implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage yanQuMessage) throws Exception {
        DunHuangShiKuProto.DunHuangShiKuSynActiveUserInfoToGameServerMsg builder = DunHuangShiKuProto.DunHuangShiKuSynActiveUserInfoToGameServerMsg.parseFrom(yanQuMessage.getBytes());
        int lastLoginTime = builder.getLastLoginTime();
        DunHuangShiKuMgr.getLogger().info("收到获取 获取活跃玩家数据 请求 activityId:{}，活跃时间 lastLoginTime:{}", builder.getActivityId(), lastLoginTime);
        if (builder.getActivityId() != DunHuangShiKuMgr.getCurActivityId()) {
            DunHuangShiKuMgr.getLogger().warn("敦煌石窟 向区服获取活跃玩家数据 无效 活动id 与当前id不一致。activityId:{} ,curActivityId:{}", builder.getActivityId(), DunHuangShiKuMgr.getCurActivityId());
            return;
        }
        Map<Long, Long> activeUserPatronsAbility = UserPatronsBussiness.getActiveUserPatronsAbility(lastLoginTime, DunHuangShiKuMgr.getDunhuangshikuActiveTitle());

        Map<Integer, DunHuangShiKuProto.DunHuangShiKuSynActiveUserInfoToCrossMsg.Builder> map = new HashMap<>();
        Map<Integer, UnionActivityGroup> groupMap = GameUnionActivityGroupMgr.getGroupMap(builder.getActivityId());
        for (Integer groupId : groupMap.keySet()) { // 预设置分组，就算没有活跃玩家也要向跨服发送数据，用于NPC血量生成
            if (!map.containsKey(groupId)) {
                DunHuangShiKuProto.DunHuangShiKuSynActiveUserInfoToCrossMsg.Builder toCrossMsg = DunHuangShiKuProto.DunHuangShiKuSynActiveUserInfoToCrossMsg.newBuilder();
                toCrossMsg.setActivityId(builder.getActivityId());
                map.put(groupId, toCrossMsg);
            }
        }

        for (Map.Entry<Long, Long> entry : activeUserPatronsAbility.entrySet()) {
            long userId = entry.getKey();
            int groupId = GameUnionActivityGroupMgr.getGroupId(builder.getActivityId(), userId);
            if (groupId == UnionActivityGroupHelper.NO_GROUP_ID) {
                continue;
            }
            if (!map.containsKey(groupId)) { // 二次校验
                DunHuangShiKuProto.DunHuangShiKuSynActiveUserInfoToCrossMsg.Builder toCrossMsg = DunHuangShiKuProto.DunHuangShiKuSynActiveUserInfoToCrossMsg.newBuilder();
                toCrossMsg.setActivityId(builder.getActivityId());
                map.put(groupId, toCrossMsg);
            }
            DunHuangShiKuProto.DunHuangShiKuActiveUserInfo.Builder activeUserInfo = DunHuangShiKuProto.DunHuangShiKuActiveUserInfo.newBuilder();
            activeUserInfo.setUserId(userId);
            activeUserInfo.setPatronsTotalAbility(entry.getValue());

            map.get(groupId).addActiveUserInfoList(activeUserInfo);
        }

        for (Map.Entry<Integer, DunHuangShiKuProto.DunHuangShiKuSynActiveUserInfoToCrossMsg.Builder> entry : map.entrySet()) {
            int groupId = entry.getKey();
            DunHuangShiKuMgr.getLogger().info("获取活跃玩家数据 activityId:{} groupId:{} 活跃玩家数据：{}", builder.getActivityId(), groupId, JSON.toJSONString(activeUserPatronsAbility));
            DunHuangShiKuProto.DunHuangShiKuSynActiveUserInfoToCrossMsg.Builder toCrossMsg = entry.getValue();
            GamePlayerMgr.sendUnionActivityGroupPacket(Protocol.C2_DUN_HUANG_SHI_KU_GET_ACTIVE_USER_INFO, toCrossMsg, 0, builder.getActivityId(), groupId);
        }
    }
}
